package org.ranshiyan.utils;

import org.springframework.stereotype.Component;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@Component
public class FileUtils {
    public static String encode(String path) throws Exception {
        try {
            File file = new File(path);
            file.getName();
            InputStream inputStream = null;
            byte[] data =null;
            try{
                inputStream = new FileInputStream(file);
                data = new byte[inputStream.available()];
                inputStream.read(data);
                inputStream.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try{
                    if (inputStream!=null){
                        inputStream.close();
                    }
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            BASE64Encoder base64Decoder = new BASE64Encoder();
            return base64Decoder.encode(data);
        }catch (Exception e){
            throw new Exception("asd");
        }

    }
}
